package com.xuanzheng.company.service.impl;

import com.xuanzheng.company.dao.AttendanceRecordDao;
import com.xuanzheng.company.dao.impl.AttendanceRecordDaoImpl;
import com.xuanzheng.company.entity.AttendanceRecord;
import com.xuanzheng.company.service.AttendanceRecordService;
import com.xuanzheng.company.service.AttendanceSettingService;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class AttendanceRecordServiceImpl implements AttendanceRecordService {
    private AttendanceRecordDao attendanceRecordDao;
    private AttendanceSettingService attendanceSettingService;
    
    public AttendanceRecordServiceImpl() {
        this.attendanceRecordDao = new AttendanceRecordDaoImpl();
        this.attendanceSettingService = new AttendanceSettingServiceImpl();
    }
    
    @Override
    public Map<String, Object> checkIn(String userId, String companyId, String employeeNumber) {
        Map<String, Object> result = new HashMap<>();
        
        // 检查是否在上班打卡时间范围内
        boolean inTimeRange = attendanceSettingService.isInCheckInTimeRange(companyId);
        
        if (!inTimeRange) {
            result.put("success", false);
            result.put("message", "不在上班打卡时间范围内");
            return result;
        }
        
        // 获取今天的日期
        Date today = Date.valueOf(LocalDate.now());
        
        try {
            // 检查今天是否已经打过卡
            AttendanceRecord record = attendanceRecordDao.getByUserIdAndDate(userId, today);
            
            if (record != null && record.getCheckInTime() != null) {
                result.put("success", false);
                result.put("message", "今天已经上班打卡，不能重复打卡");
                return result;
            }
            
            // 创建或更新打卡记录
            if (record == null) {
                // 创建新的打卡记录
                record = new AttendanceRecord();
                record.setUserId(userId);
                record.setCompanyId(companyId);
                record.setEmployeeNumber(employeeNumber);
                record.setAttendanceDate(today);
                
                // 获取当前时间
                LocalDateTime now = LocalDateTime.now();
                Timestamp currentTime = Timestamp.valueOf(now);
                
                record.setCheckInTime(currentTime);
                
                if (attendanceRecordDao.save(record) > 0) {
                    result.put("success", true);
                    result.put("message", "上班打卡成功");
                    result.put("data", record);
                } else {
                    result.put("success", false);
                    result.put("message", "上班打卡失败，请稍后重试");
                }
            } else {
                // 更新现有的打卡记录
                // 获取当前时间
                LocalDateTime now = LocalDateTime.now();
                Timestamp currentTime = Timestamp.valueOf(now);
                
                record.setCheckInTime(currentTime);
                
                if (attendanceRecordDao.update(record) > 0) {
                    result.put("success", true);
                    result.put("message", "上班打卡成功");
                    result.put("data", record);
                } else {
                    result.put("success", false);
                    result.put("message", "上班打卡失败，请稍后重试");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "系统错误，请稍后重试");
        }
        
        return result;
    }
    
    @Override
    public Map<String, Object> checkOut(String userId, String companyId) {
        Map<String, Object> result = new HashMap<>();
        
        // 检查是否在下班打卡时间范围内
        boolean inTimeRange = attendanceSettingService.isInCheckOutTimeRange(companyId);
        
        if (!inTimeRange) {
            result.put("success", false);
            result.put("message", "不在下班打卡时间范围内");
            return result;
        }
        
        // 获取今天的日期
        Date today = Date.valueOf(LocalDate.now());
        
        // 检查今天是否已经上班打卡
        AttendanceRecord record = attendanceRecordDao.getByUserIdAndDate(userId, today);
        
        if (record == null) {
            result.put("success", false);
            result.put("message", "今天还未上班打卡，请先进行上班打卡");
            return result;
        }
        
        if (record.getCheckInTime() == null) {
            result.put("success", false);
            result.put("message", "今天还未上班打卡，请先进行上班打卡");
            return result;
        }
        
        // 检查今天是否已经下班打卡
        if (record.getCheckOutTime() != null) {
            result.put("success", false);
            result.put("message", "今天已经下班打卡，不能重复打卡");
            return result;
        }
        
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        Timestamp currentTime = Timestamp.valueOf(now);
        
        // 更新打卡记录
        record.setCheckOutTime(currentTime);
        
        if (attendanceRecordDao.update(record) > 0) {
            result.put("success", true);
            result.put("message", "下班打卡成功");
            result.put("data", record);
        } else {
            result.put("success", false);
            result.put("message", "下班打卡失败，请稍后重试");
        }
        
        return result;
    }
    
    @Override
    public AttendanceRecord getTodayRecord(String userId) {
        // 获取今天的日期
        Date today = Date.valueOf(LocalDate.now());
        
        // 直接从数据库查询记录
        AttendanceRecord record = attendanceRecordDao.getByUserIdAndDate(userId, today);
        
        return record;
    }
}